<?php

namespace App\Http\Controllers\API\Notification;

use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\API\APIController;
use App\Models\API\Notification;

class NotificationController extends APIController
{
    public function index()
    {
        $userId = Auth::user()->id;
    	$notifications = Notification::where('user_id', $userId)->paginate();
    	return response()->json($this->setJson(true, $notifications), 200);
    }

    // 用户查看通知 - status=1
    public function check($id)
    {
    	$notification = Notification::find($id);
    	$notification->status = 1;
    	$notification->save();
    	return response()->json($this->setJson(true, 'check'), 200);
    }

    public function destroy(Request $request, $id)
    {
    	$deleteNotification = Notification::find($id);
    	$deleteNotification->delete();
    	return response()->json($this->setJson(true, 'delete_succeed'), 200);
    }

    private function setJson($status, $msg)
    {
        $res = [
            'status' => $status,
            'msg'    => $msg
        ];
        return $res;
    }
}
